package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.xiaoma.myaudience.biz.model.ActivityInfoModel;
import com.xiaoma.myaudience.biz.model.UserAccount;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.PrefHelper;
import com.xiaoma.myaudience.util.model.ModelUtils;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

public class UserInfoDetailTask extends
		BaseDataAsyncTask<Map<String,String>, Void, Map<String, Object>> {
	private final static String TAG = "UserInfoDetailTask";
	public UserInfoDetailTask(Context context, int taskType, DataAsyncCallback callback) {
		super(context, taskType, callback);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected Map<String, Object> doInBackground(Map<String,String>... params) {
		//get eventid from params
		Map<String, Object> result= new HashMap<String, Object>();
		Map<String, String> cookie = null;
		JSONObject jsonobj =null;
		if(params!=null){
			cookie = params[0];
		}
		try { 
			String cookiestr = Constant.LOGIN_COOKIE1+"="+cookie.get(Constant.LOGIN_COOKIE1)+";"+Constant.LOGIN_COOKIE2+"="+cookie.get(Constant.LOGIN_COOKIE2);
			Header[] headers = new BasicHeader[2];//("X-Online-Host",  format_CMWAP_ServerName(strPath));  
			headers[0]=new BasicHeader("Cookie", cookiestr);
			headers[1]=new BasicHeader("Accept-Language","zh-CN");
			List<NameValuePair> loginParams = new ArrayList<NameValuePair>();
        	loginParams.add(new BasicNameValuePair("status", "2"));
        	HttpResponse httpResponse = HttpUtils.doHttpExecute(getHttpClient(), Constant.URL_GET_USERINFO, loginParams,headers, HttpUtils.POST,"GBK");
        	if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        		String responseStr = EntityUtils.toString(httpResponse.getEntity(),"GBK");
				String code = "";
				if(responseStr!=null){
					try {
						 jsonobj = new JSONObject(responseStr);
						 code=jsonobj.getString("code");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				if(!code.equals("0")){
					//Tips("网站没有响应");测试用成功
					result.put(Constant.LOGIN_ISLOGIN, false);
				}else{
					result.put(Constant.LOGIN_ISLOGIN, true);
					JSONObject data = jsonobj.getJSONObject("data");
					JSONObject userinfo = data.getJSONObject("userinfo");
					result.put("userinfo", userinfo);
					}
				}
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
        } finally {
            closeHttpClient();
        }
		
		return result;
	}
	

}
